// JavaScript Document

	var ta_map;
	var geocoder;
	var ta_directionsService = new google.maps.DirectionsService();
	var from;
	var to;
	var i2 = -1;
  
	function taxi_calcRoute(nextorprev,mapornomap) 
	{
		directionsDisplay = new google.maps.DirectionsRenderer();
		var opts = 
		{
		 //center: new google.maps.LatLng(28.631257700000000000, 77.223091299999960000),
		 zoom: 12,
		 mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		ta_map = new google.maps.Map(document.getElementById('taxi_map'), opts);
		google.maps.event.trigger(ta_map,'resize');
			
		//directionsDisplay.setPanel(document.getElementById('directions-panel'));
		 
		var trafficLayer = new google.maps.TrafficLayer();
        trafficLayer.setMap(ta_map);

		geocoder = new google.maps.Geocoder();
	  		
		var origin1 = document.getElementById("from").value;
		var destinationA = document.getElementById("to").value;
		var distanceInput_main = document.getElementById("taximain_outputdist");
		var distanceInput_map = document.getElementById("taximap_outputdist");
		$('#taximain_outputdist').empty();
		$('#taximap_outputdist').empty();
		var durationInput_main = document.getElementById('taximain_outputtime');
		var durationInput_map = document.getElementById('taximap_outputtime');
		$('#taximain_outputtime').empty();
		$('#taximap_outputtime').empty();
			
		directionsDisplay.setMap(ta_map);
        //var selectedMode = document.getElementById('mode').value;
        var request =
		{
			origin: origin1,
			destination: destinationA,
			travelMode: google.maps.TravelMode.DRIVING,
			provideRouteAlternatives: true,
			durationInTraffic: true
        };
		
		ta_directionsService.route(request, function(response, status) 
		{
	      	if (status == google.maps.DirectionsStatus.OK) 
			{	  
				if(nextorprev==1)
					i2++;
				else if(nextorprev==-1)
					i2--;
				/*if(response.routes.length < i+1)
								{
									console.log("No such route");
									i=-1;
								}
				 // Display the distance:
				 else{*/
		 
				if(response.routes.length < i2+1)
					i2=0;
				if(i2==-1)
				{
					i2 = response.routes.length - 1;
				}
				if(i2<=-2)
				{
					i2=-1;
					//console.log("Levelof stupidity: Above 9000");
				}
         		document.getElementById('taximain_outputdist').innerHTML += 
            	response.routes[i2].legs[0].distance.text;
         		document.getElementById('taximap_outputdist').innerHTML += 
            	response.routes[i2].legs[0].distance.text;

        		// Display the duration:
         		document.getElementById('taximain_outputtime').innerHTML += 
            	response.routes[i2].legs[0].duration.text;
         		document.getElementById('taximap_outputtime').innerHTML += 
            	response.routes[i2].legs[0].duration.text;
				
				var strdistance = document.getElementById('taximain_outputdist').innerHTML;
				var distance = parseFloat(strdistance);

				//console.log("taxi distance is: " + distance);
				
				//---------------------------------FARE CALCULATION----------------------------------------
				
				/* MERU EASY FARE: 69 for first 3km, 23 per km after 1st 3 km 
				   QUICK: 12.5 per km
				*/
				
				var meru_easy_fare = 0;
				var quick_fare = 0;
				
				if (distance <= 3)
				{
					meru_easy_fare = 69;
					quick_fare = 10;
				}
				else
				{
					var distanceleft = distance-3;
					meru_easy_fare = 69 + distanceleft*23;

				}
				
				/* QUICK CAB: Rs. 50/booking convenience charge
				   Rs. 12.5/km
				*/
				var fare_meru = 0;
				fare_meru = Math.round(meru_easy_fare);
				quick_fare = 50 + distance*12;
				
				//meru_easy_roundedfare = Math.round(meru_easy_fare * 100) / 100;
				document.getElementById('taximain_easy_fare').innerHTML = fare_meru + " - Meru, Easy \n";
				/*document.getElementById('taximain_easy_fare').innerHTML = "Easy cabs: Rs. " + meru_easy_fare + "\n";*/
				document.getElementById('taximain_quick_fare').innerHTML = Math.round(quick_fare) + " - Quick Cabs\n";
				document.getElementById('taximap_meru_fare').innerHTML = "Meru cabs: Rs. " + fare_meru;	
				document.getElementById('taximap_easy_fare').innerHTML = "Easy cabs: Rs. " + fare_meru;	
				document.getElementById('taximap_quick_fare').innerHTML = "Quick cabs: Rs. " + Math.round(quick_fare);										
				
				//------------------------------------------------------------------------------------------		
				
			
				//For duration in traffic need to buy Business maps API
				
				//distanceInput.value = response.routes[i].legs[0].distance.text;
				//durationInput.value = response.routes[i].legs[0].duration.text;
				
				//localStorage.setItem('dist', distanceInput.value);
				//localStorage.setItem('time', durationInput.value);

				//directionsDisplay.setRouteIndex(1);
                //console.log(directionsDisplay.getRouteIndex());
                //var temp = directionsDisplay.getDirections();
						
				if(i2==0)
				{
					if(mapornomap==1)
					{
						directionsDisplay.setDirections(response);
						google.maps.event.trigger(ta_map,'resize');
					}
							//directionsDisplay.setDirections(response);
				}
				if(i2>=1)
				{
					if(mapornomap==1)
					{
						var temp = response.routes[0];
						response.routes[0]= response.routes[i2];
						response.routes[i2]=temp;
						directionsDisplay.setDirections(response);
						google.maps.event.trigger(ta_map,'resize');
					}
					else if(mapornomap==0)
					{
						var temp = response.routes[0];
						response.routes[0]= response.routes[i2];
						response.routes[i2]=temp;
					}
					//directionsDisplay.setDirections(response);
				}
                //console.log(directionsDisplay.getRouteIndex());
			}
      //directionsDisplay.setDirections(response);
	  });
	}	  
	//google.maps.event.addDomListener(window, 'load', initialize);
	
	function taxi_getdirections()
	{
		directionsDisplay.setPanel(document.getElementById('taxi_directions-panel'));
	}